from google.appengine.ext import db
from google.appengine.api import users

from django.db.models import permalink
from django.core.urlresolvers import reverse

from datetime import date

class Squadron(db.Model):
  number = db.StringProperty(required=True)
  
  def __unicode__(self):
    return '%s Squadron' % (self.number)
  
  def view_url(self):
    return reverse('sqn-view', kwargs={'number':self.number})
  
  def edit_url(self):
    return reverse('sqn-edit', kwargs={'number':self.number})
  
  def delete_url(self):
    return reverse('sqn-delete', kwargs={'number':self.number})
  
  @classmethod
  def new_url(cls):
    return reverse('sqn-delete')
  
  @classmethod
  def list_url(cls):
    return reverse('sqn-list')
  
  @classmethod
  def get_by_number(cls, number):
    """ Returns a single model instance with the given name """
    sqn = cls.all()
    sqn.filter('number =', number)
    return sqn.get()
  

class School(db.Model):
  name = db.StringProperty(required=True)

class Flight(db.Model):
  name = db.StringProperty(required=True)
  squadron = db.ReferenceProperty(required=True)

class Cadet(db.Model):
  first_name = db.StringProperty(required=True)
  last_name = db.StringProperty(required=True)
  # initials = db.StringProperty()
  # know_as = db.StringProperty()
  # 
  # birthday = db.DateProperty(required=True)
  # gender = db.StringProperty(required=True,
  #                            choices=['Male', 'Female'])
  # nationality = db.StringProperty()
  # religion = db.StringProperty()
  # occupation = db.StringProperty()
  # 
  # school = db.ReferenceProperty(School)
  # service_number = db.IntegerProperty(required=True)
  # flight = db.ReferenceProperty(Flight)
  # 
  # basic_date = db.DateProperty()
  # status = db.StringProperty()
  # 
  # address = db.StringProperty(required=True,
  #                             multiline=True)
  
  def __unicode__(self):
    return '%s %s' % (self.first_name, self.last_name)
    
  def view_url(self):
    return reverse('cadet-view', kwargs={'first_name':self.first_name, 'last_name':self.last_name})
  
  def edit_url(self):
    return reverse('cadet-edit', kwargs={'first_name':self.first_name, 'last_name':self.last_name})
  
  def delete_url(self):
    return reverse('cadet-delete', kwargs={'first_name':self.first_name, 'last_name':self.last_name})
  
  @classmethod
  def new_url(cls):
    return reverse('cadet-delete')
  
  @classmethod
  def list_url(cls):
    return reverse('cadet-list')
  
  @classmethod
  def get_by_name(cls, first_name=None, last_name=None):
    """ Returns a single model instance with the given name """
    cadets = cls.all()
    if not (first_name or last_name):
      raise ValueError('You must supply at least a first or last name')
    if first_name:
      cadets.filter('first_name =', first_name)
    if last_name:
      cadets.filter('last_name =', last_name)
    return cadets.get()
  
    return self.actions[action]